/*
 * init.cpp
 *
 *  Created on: May 11, 2011
 *      Author: jmurray
 */
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <boost/tokenizer.hpp>

#include "init.hpp"

using namespace std;

int init_map(vector<Block*>& BlockMap,vector<RFID_tag*>& TagMap){
	//Namespaces (if needed)
	using namespace boost;
	
	//Vraiables
	ifstream myfile;
	string line;
	int count=0;
	int tmp_ID;
	int index;
	char tmp_status;
	string tmp_name;
	vector<int> tmp_links;
	vector<int> tmp_tags;
	bool state=true;
	
	//Open File
	myfile.open("data/map_b.csv",ios::in);
	if (myfile.is_open() && myfile.good())
		cout << "File opened, and is good!" << endl;
	else 
	{
		cout << "Error at " << __LINE__ << endl;
		return -1;
	}
	
	//First loop to grab ID's
	while (1)
	{
		getline(myfile,line);
		count++;
		state=true;
		tmp_links.clear();
		tmp_tags.clear();
		
		if (myfile.eof())
			break;
		//Check for a comment line
		if (line.at(0)=='#' || line.at(0)>57 || line.at(0)<48)
			continue;
		
		//We have a good line, tokenize!
		tokenizer<escaped_list_separator<char> > tok(line);
		tokenizer<escaped_list_separator<char> >::iterator beg=tok.begin();
		
		//Parse initial data
		tmp_ID=atoi((*beg++).c_str());		//ID
		if (beg==tok.end())
		{
			cout << "Error on line " << count << ", name is corrupt!" << endl;
			continue;
		}
		tmp_name=*beg++;			//Name
		if (beg==tok.end())
		{
			cout << "Error on line " << count << ", status is corrupt!" << endl;
			continue;
		}
		tmp_status=atoi((*beg++).c_str());	//Status
		
		//Parse data for the Block ID List
		tmp_links.push_back(atoi((*beg++).c_str()));
		for(int i=0;i<tmp_links[0];i++)
		{
			if (beg==tok.end())
			{
				cout << "Error on line " << count << ", there are not " << tmp_tags[0] << " blocks in the row!" << endl;
				state=false;
				break;
			}
			tmp_links.push_back(atoi((*beg++).c_str()));
		}
		if (state==false)
			continue;
		
		//Parse date from the Tag ID List
		tmp_tags.push_back(atoi((*beg++).c_str()));
		for(int i=0;i<tmp_tags[0];i++)
		{
			if (beg==tok.end())
			{
				cout << "Error on line " << count << ", there are not " << tmp_tags[0] << " tags in the row!" << endl;
				state=false;
				break;
			}
			tmp_tags.push_back(atoi((*beg++).c_str()));
		}
		if (state==false)
			continue;
		
		//Insure that we are now at the end of the token list, all data should now be processed
		if (beg!=tok.end())
		{
			cout << "Error on line " << count << ", invalid block/tag data!" << endl;
			continue;
		}
		
		
		//Error checking is complete, enter the known information
		
		BlockMap.insert(BlockMap.end(),new Block);
		index=BlockMap.size()-1;
		BlockMap.at(index)->ID=tmp_ID;
		BlockMap.at(index)->name=tmp_name;
		BlockMap.at(index)->status=tmp_status;
		
		
		//DEBUG INFO: Print out the token now that we are reasonably assured that we are error free. 
		#ifdef DEBUG
		cout << " TOKENS: Line " << count << endl;
		cout << "ID: " << tmp_ID << endl;
		cout << "Name: " << tmp_name << endl;
		cout << "Status: " << (int)tmp_status << endl;
		cout << "Block Links: " << endl;
		for(int i=0;i<tmp_links[0];i++)
		{
			cout << "   Link " << i << "=" << tmp_links[i+1] << endl;
		}
		cout << "Tag Links: " << endl;
		for(int i=0;i<tmp_tags[0];i++)
		{
			cout << "   Link " << i << "=" << tmp_tags[i+1] << endl;
		}
		cout << endl << endl;
		#endif
		//TODO: Run second interation of BLOCK and TAG input from the BlockMap in init.cpp to generate the actual links in the vectors.
		//TODO: Look up and use "strtoq" in init.cpp to grab 64bit long ints! woot!
	}
	myfile.close();
	return 0;
	
};
